package com.lx.work2;

import com.sun.xml.internal.ws.addressing.WsaTubeHelper;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author c先生
 * @date 2022年04月06日 下午 7:32:27
 * @describe
 */

public class UserMain {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        UserManager um = new UserManager();
        int key = 0;
        while (true) {
            System.out.println("1、注册");
            System.out.println("2、登录");
            System.out.println("请输入你的选择");
            key = sc.nextInt();
            switch (key) {
                case 1:
                    System.out.println("请输入用户名：");
                    String userName = sc.next();
                    List<User> users = um.findAllUser();
                    boolean flag = false;
                    if(users == null){
                        users = new ArrayList<>();
                    }
                    for (User user : users) {
                        if(user != null){
                            if(user.getUserName().equals(userName)){
                                flag = true;
                                break;
                            }
                        }
                    }
                    if(flag){
                        System.out.println("该用户已存在！");
                        break;
                    }else {
                        System.out.println("请输入密码：");
                        String password = sc.next();
                        User user = new User(userName,password);
                        users.add(user);
                    }
                    boolean b = um.regUser(users);
                    if(b){
                        System.out.println("注册成功！");
                    }else {
                        System.out.println("注册失败！");
                    }
                    break;
                case 2:
                    int i = 0;
                    boolean flag1 = false;
                    while (i<3){
                        System.out.println("请输入用户名：");
                        String name = sc.next();
                        System.out.println("请输入密码：");
                        String psw = sc.next();
                        List<User> list = um.findAllUser();
                        for (User user : list) {
                            if(user.getUserName().equals(name) && user.getPassWord().equals(psw)){
                                flag1 = true;
                                break;
                            }
                        }
                        if(flag1 == true){
                            break;
                        }
                        i++;
                    }
                    if(flag1 == true){
                        System.out.println("登录成功！");
                        StudentManager sm = new StudentManager();
                        while (true){
                            System.out.println("学生管理系统");
                            System.out.println("1、查询所有学生");
                            System.out.println("2、删除一名学生");
                            System.out.println("3、修改一名学生");
                            System.out.println("4、新增一名学生");
                            switch (sc.nextInt()){
                                case 1 :
                                    sm.showAllStudent();
                                    break;
                                case 2 :
                                    System.out.println("请输入学号：");
                                    sm.deleteStudent(sc.next());
                                    break;
                                case 3 :
                                    System.out.println("请输入学号：");
                                    sm.updateStudent(sc.next());
                                    break;
                                case 4 :
                                    System.out.println("请输入学号、姓名、性别：");
                                    Student student = new Student(sc.next(),sc.next(),sc.next());
                                    sm.addStudent(student);
                                    break;
                                default:
                                    System.exit(0);
                            }
                        }
                    }else {
                        System.exit(0);
                    }
                    break;
                default:
                    System.exit(0);
            }
        }
    }
}
